CS 211 Lesson 6

Branches

Quote:

Integrity is the fundamental premise for military service in a free society.  Without integrity, the moral pillars of our military strength, public trust, and self-respect are lost. Charles A. Gabriel

Lesson Objectives:

Lesson:

I. MATLAB Concepts

A. Control Structures

B. The if statement

  1. MATLAB's if statement has several different forms which may be categorized as:

    • one-alternative if

    • two-alternative if

    • multiple-alternative if with else

    • multiple-alternative if without else
       

    • For a given problem, use the simplest form that will accomplish your task.
       

  2. The one-alternative if has the following format (syntax):

    if  <logical expression>
        <one or more statements>

    end
     

    • For example:

      if Body_temp > 98.6
          fprintf('The patient has a fever.\n')
      end

       

    • The one-alternative if statement behaves as follows:

      If the logical expression evaluates to true, the statement(s) following the logical expression and before the end are executed.  Otherwise (i.e., the logical expression evaluates to false), the statement(s) following the expression and before the end are not executed and execution skips to the statements following the end.
       

    • While not required, indenting the lines between the if and end lines is very important for readability
       

  3. The two-alternative if has the following format (syntax):

    if  <logical expression>
        <one or more statements>

    else
        <one or more statements>
    end

     

    • For example:

      if Body_temp > 98.6
          fprintf('The patient has a fever.\n')
      else
          fprintf('The patient does not have a fever.\n');
      end

       

    • The two-alternative if statement behaves as follows:

      If the logical expression evaluates to true, the statement(s) following the logical expression and before the else are executed and execution skips to the statements following the end.  Otherwise (i.e., the logical expression evaluates to false), the statement(s) before the else are skipped and the statements after the else and before the end are executed and execution continues with statements following the end.
       

    • With the two-alternative if, either the code before the else or the code after the else will always be executed (but never both).

    • Never put a logical expression immediately following the else. (MATLAB will ignore the expression if you include one.)
       

  4. The multiple-alternative if with else has the following format (syntax):

    if  <logical expression>
        <one or more statements>

    elseif <logical expression>
        <one or more statements>
    <zero or more additional elseif blocks like the one above>
    else
        <one or more statements>

    end

     

    • For example:

      if Body_temp >= 102
          fprintf('The patient has a high fever.\n')
      elseif Body_temp >= 99
          fprintf('The patient has a low grade fever.\n');
      elseif
      Body_temp >= 96
          fprintf('The patient has a normal body temperature.\n');
      elseif
      Body_temp >= 92
          fprintf('The patient has a low body temperature.\n');
      else
          fprintf('The patient is hypothermic.\n');
      end

       

    • The multiple-alternative if with else behaves as follows:

      The logical expressions are evaluated in order from top to bottom.  The code immediately following the first logical expression that evaluates to true, if any do, is executed (up to the next elseif or the else) and then execution skips to the code following the end.  If none of the logical expressions evaluate to true, the statements between the else and before the end are executed and execution continues with statements following the end.
       

    • One statement block always executes when using a multiple-alternative if with else statement.

    • Use a multiple-alternative if with else statement when your code should do something for all possible cases.
       

  5. The multiple-alternative if without else has the following format (syntax):

    if  <logical expression>
        <one or more statements>

    elseif <logical expression>
        <one or more statements>
    <zero or more additional elseif blocks like the one above>

    end

     

    • For example:

      if Body_temp >= 102
          fprintf('The patient has a high fever.\n')
      elseif Body_temp > 98.6
          fprintf('The patient has a low grade fever.\n');
      elseif
      Body_temp < 95
          fprintf('The patient has a low body temperature.\n');
      end

       

    • The multiple-alternative if without else behaves as follows:

      The logical expressions are evaluated in order from top to bottom.  The code immediately following the first logical expression that evaluates to true is executed (up to the next elseif or the end) and then execution skips to the code following the end.  If none of the logical expressions evaluate to true, none of the statement(s) are executed and execution resumes with statements following the end.
       

    • Either one statement block or no statement block will execute using a multiple-alternative if without else statement.

    • Use a multiple-alternative if without else statement when the code should take no action in at least one case.
       

  6. if statements can be nested inside an if statement. For example:

    if Body_temp >= 102
        fprintf('The patient has a high fever.\n')
        if Body_weight > 200 && Body_height < 64
          fprintf('Send the patient to the hospital\n');
        end
    elseif Body_temp > 98.6
        fprintf('The patient has a low grade fever.\n');
    elseif
    Body_temp < 95
        fprintf('The patient has a low body temperature.\n');
        if Temperature_time > 90
          fprintf('Send the patient to the hospital\n');
        end
    end

  7. Design your if statements in such a way as to minimize logical tests.
     

  8. Make sure your if statements do not include redundant tests. This makes your code more difficult to understand, harder to maintain over time, and slower to execute. The following is an example of a poorly-designed if statement with 4 unnecessary redundant tests.

    if Score >= 90
       disp('A')
    elseif Score >= 80 & Score < 90
       disp('B')
    elseif Score >= 70 & Score < 80
       disp('C')
    elseif Score >= 60 & Score < 70
       disp('D')
    elseif Score < 60
       disp('F')
    end

A better design without redundant tests is:

if Score >= 90
   disp('A')
elseif Score >= 80
   disp('B')
elseif Score >= 70
   disp('C')
elseif Score >= 60
   disp('D')
else
   disp('F')
end

The key to avoiding redundant tests is to avoid testing for cases that have already been confirmed in earlier tests.

C. The switch statement

D. The try/catch statement

II. Good Programming Practices

III. Algorithms

Lab Work: Lab 6

References:  Chapman Textbook: section 3.4, branch_demos.m